﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Data;
using System.Windows.Controls;
using System.Windows;
using System.Runtime.Serialization;
using System.Net;


namespace WPFRadio
{
    [Serializable]
    
    public class Category
    {
         
        private bool _Loaded = false;

      
        public int IDCategory { get; set; }

     
        public string name { get; set; }
       
        public string genre { get; set; }
        
        public string station { get; set; }
        
       
   
        public CompositeCollection Children { get; set; }

        public Category(string name)
        {
            Children = new CompositeCollection();
            Children.Add(new TextBlock()
            {
                Text = "Loading...",
                FontStyle = FontStyles.Italic
            });
            this.name = name;
        }

        public static List<Category> DeserializeXml(Stream stream)
        {
            var Data = new DataContractSerializer(typeof(List<Category>));
            return Data.ReadObject(stream) as List<Category>;
        }

        public  void LoadChildren()
        {
            if (_Loaded)
            { return; }

            _Loaded = false;

            var wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri(PlaylistManager.UrlStationByGenre + PlaylistManager.DevID + "&genre=" + name));


        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null || e.Cancelled)
            {
                ((TextBlock)Children[0]).Text = "Error Getting Category Children";
                return;
            }


        }
    }
}
